home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / c / CLib-SDI.lha / CLib-SDI / libsource / libinit.c < prev    next >
C/C++ Source or Header  |  2002-10-27  |  8KB  |  265 lines

  1. #ifndef EXAMPLE_LIBINIT_C
  2. #define EXAMPLE_LIBINIT_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        libinit.c
  7.     Main:        example
  8.     Versionstring:    $VER: libinit.c 1.1 (21.09.2002)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    all the library initialization stuff
  12.  
  13.  1.0   25.06.00 : first version
  14.  1.1   21.09.02 : added 3rd function
  15. */
  16.  
  17. #include <exec/execbase.h>
  18. #include <exec/resident.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <proto/exec.h>
  21. #include <proto/utility.h>
  22. #include "libinfo.h"
  23.  
  24. #ifdef __MORPHOS__
  25. #ifndef RTF_PPC
  26. #define RTF_PPC (1<<3) /* rt_Init points to a PPC function */
  27. #endif
  28. #ifndef FUNCARRAY_32BIT_QUICK_NATIVE
  29. #define FUNCARRAY_32BIT_QUICK_NATIVE 0xFFFBFFFB
  30. #endif
  31. /* To tell the loader that this is a new emulppc elf and not
  32.  * one for the ppc.library. */
  33. ULONG __amigappc__=1;
  34. #endif
  35.  
  36. /************************************************************************/
  37.  
  38. /* First executable routine of this library; must return an error
  39.    to the unsuspecting caller */
  40. LONG ReturnError(void)
  41. {
  42.   return -1;
  43. }
  44.  
  45. /************************************************************************/
  46.  
  47. /* natural aligned! */
  48. struct LibInitData {
  49.  UBYTE i_Type;     UBYTE o_Type;     UBYTE  d_Type;    UBYTE p_Type;
  50.  UWORD i_Name;     UWORD o_Name;     STRPTR d_Name;
  51.  UBYTE i_Flags;    UBYTE o_Flags;    UBYTE  d_Flags;    UBYTE p_Flags;
  52.  UBYTE i_Version;  UBYTE o_Version;  UWORD  d_Version;
  53.  UBYTE i_Revision; UBYTE o_Revision; UWORD  d_Revision;
  54.  UWORD i_IdString; UWORD o_IdString; STRPTR d_IdString;
  55.  ULONG endmark;
  56. };
  57.  
  58. /************************************************************************/
  59. extern const ULONG LibInitTable[4]; /* the prototype */
  60.  
  61. /* The library loader looks for this marker in the memory
  62.    the library code and data will occupy. It is responsible
  63.    setting up the Library base data structure. */
  64. const struct Resident RomTag = {
  65.   RTC_MATCHWORD,                   /* Marker value. */
  66.   (struct Resident *)&RomTag,      /* This points back to itself. */
  67.   (struct Resident *)LibInitTable, /* This points somewhere behind this marker. */
  68. #ifdef __MORPHOS__
  69.   RTF_PPC|
  70. #endif
  71.   RTF_AUTOINIT,                    /* The Library should be set up according to the given table. */
  72.   VERSION,                         /* The version of this Library. */
  73.   NT_LIBRARY,                      /* This defines this module as a Library. */
  74.   0,                               /* Initialization priority of this Library; unused. */
  75.   LIBNAME,                         /* Points to the name of the Library. */
  76.   IDSTRING,                        /* The identification string of this Library. */
  77.   (APTR)&LibInitTable              /* This table is for initializing the Library. */
  78. };
  79.  
  80. /************************************************************************/
  81.  
  82. /* The mandatory reserved library function */
  83. static ULONG LibReserved(void)
  84. {
  85.   return 0;
  86. }
  87.  
  88. /* Open the library, as called via OpenLibrary() */
  89. static ASM(struct Library *) LibOpen(REG(a6, struct ExampleBaseP * ExampleBase))
  90. {
  91.   /* Prevent delayed expunge and increment opencnt */
  92.   ExampleBase->exb_LibNode.lib_Flags &= ~LIBF_DELEXP;
  93.   ExampleBase->exb_LibNode.lib_OpenCnt++;
  94.  
  95.   return &ExampleBase->exb_LibNode;
  96. }
  97.  
  98. #ifdef BASE_GLOBAL
  99. struct ExecBase *      SysBase       = 0;
  100. struct IntuitionBase * IntuitionBase = 0;
  101. struct UtilityBase *   UtilityBase   = 0;
  102. struct ExampleBase *   ExampleBase   = 0;
  103.  
  104. static void MakeGlobalLibs(struct ExampleBaseP *exampleBase)
  105. {
  106.   IntuitionBase = exampleBase->exb_IntuitionBase;
  107.   UtilityBase =   exampleBase->exb_UtilityBase;
  108.   ExampleBase   = (struct ExampleBase *) exampleBase;
  109. }
  110. static void MakeGlobalSys(struct ExampleBaseP *exampleBase)
  111. {
  112.   SysBase = exampleBase->exb_SysBase;
  113. }
  114. #endif
  115.  
  116. /* Closes all the libraries opened by LibInit() */
  117. static void CloseLibraries(struct ExampleBaseP * ExampleBase)
  118. {
  119. #ifndef BASE_GLOBAL
  120.   struct ExecBase *SysBase = ExampleBase->exb_SysBase;
  121. #endif
  122.  
  123.   if(ExampleBase->exb_IntuitionBase)
  124.     CloseLibrary((struct Library *) ExampleBase->exb_IntuitionBase);
  125.   if(ExampleBase->exb_UtilityBase)
  126.     CloseLibrary((struct Library *) ExampleBase->exb_UtilityBase);
  127. }
  128.  
  129. /* Expunge the library, remove it from memory */
  130. static ASM(SEGLISTPTR) LibExpunge(REG(a6, struct ExampleBaseP * ExampleBase))
  131. {
  132. #ifndef BASE_GLOBAL
  133.   struct ExecBase *SysBase = ExampleBase->exb_SysBase;
  134. #endif
  135.  
  136.   if(!ExampleBase->exb_LibNode.lib_OpenCnt)
  137.   {
  138.     SEGLISTPTR seglist;
  139.  
  140.     seglist = ExampleBase->exb_SegList;
  141.  
  142.     CloseLibraries(ExampleBase);
  143.  
  144.     /* Remove the library from the public list */
  145.     Remove((struct Node *) ExampleBase);
  146.  
  147.     /* Free the vector table and the library data */
  148.     FreeMem((STRPTR) ExampleBase - ExampleBase->exb_LibNode.lib_NegSize,
  149.     ExampleBase->exb_LibNode.lib_NegSize +
  150.     ExampleBase->exb_LibNode.lib_PosSize);
  151.  
  152.     return seglist;
  153.   }
  154.   else
  155.     ExampleBase->exb_LibNode.lib_Flags |= LIBF_DELEXP;
  156.  
  157.   /* Return the segment pointer, if any */
  158.   return 0;
  159. }
  160.  
  161. /* Close the library, as called by CloseLibrary() */
  162. static ASM(SEGLISTPTR) LibClose(REG(a6, struct ExampleBaseP * ExampleBase))
  163. {
  164.   if(!(--ExampleBase->exb_LibNode.lib_OpenCnt))
  165.   {
  166.     if(ExampleBase->exb_LibNode.lib_Flags & LIBF_DELEXP)
  167.       return LibExpunge(ExampleBase);
  168.   }
  169.   return 0;
  170. }
  171.  
  172. /* Initialize library */
  173. #ifdef __MORPHOS__
  174. static struct Library * LibInit(struct ExampleBaseP * ExampleBase,
  175. SEGLISTPTR seglist, struct ExecBase *SysBase)
  176. #else
  177. static ASM(struct Library *) LibInit(REG(a0, SEGLISTPTR seglist),
  178. REG(d0, struct ExampleBaseP * ExampleBase), REG(a6, struct ExecBase *SysBase))
  179. #endif
  180. {
  181. #ifdef _M68060
  182.   if(!(SysBase->AttnFlags & AFF_68060))
  183.     return 0;
  184. #elif defined (_M68040)
  185.   if(!(SysBase->AttnFlags & AFF_68040))
  186.     return 0;
  187. #elif defined (_M68030)
  188.   if(!(SysBase->AttnFlags & AFF_68030))
  189.     return 0;
  190. #elif defined (_M68020)
  191.   if(!(SysBase->AttnFlags & AFF_68020))
  192.     return 0;
  193. #endif
  194.  
  195.   /* Remember stuff */
  196.   ExampleBase->exb_SegList = seglist;
  197.   ExampleBase->exb_SysBase = SysBase;
  198.  
  199. #ifdef BASE_GLOBAL
  200.   MakeGlobalSys(ExampleBase);
  201. #endif
  202.  
  203.   if((ExampleBase->exb_IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  204.   {
  205.     if((ExampleBase->exb_UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 37)))
  206.     {
  207. #ifdef BASE_GLOBAL
  208.       MakeGlobalLibs(ExampleBase);
  209. #endif
  210.       return &ExampleBase->exb_LibNode;
  211.     }
  212.     CloseLibraries(ExampleBase);
  213.   }
  214.  
  215.   /* Free the vector table and the library data */
  216.   FreeMem((STRPTR) ExampleBase - ExampleBase->exb_LibNode.lib_NegSize,
  217.   ExampleBase->exb_LibNode.lib_NegSize +
  218.   ExampleBase->exb_LibNode.lib_PosSize);
  219.  
  220.   return 0;
  221. }
  222.  
  223. /************************************************************************/
  224.  
  225. /* This is the table of functions that make up the library. The first
  226.    four are mandatory, everything following it are user callable
  227.    routines. The table is terminated by the value -1. */
  228.  
  229. static const APTR LibVectors[] = {
  230. #ifdef __MORPHOS__
  231.   (APTR) FUNCARRAY_32BIT_QUICK_NATIVE,
  232. #endif
  233.   (APTR) LibOpen,
  234.   (APTR) LibClose,
  235.   (APTR) LibExpunge,
  236.   (APTR) LibReserved,
  237.   (APTR) LIBex_TestRequest,
  238.   (APTR) LIBex_TestRequest2A,
  239.   (APTR) LIBex_TestRequest3,
  240.   (APTR) -1
  241. };
  242.  
  243. static const struct LibInitData LibInitData = {
  244.  0xA0,   (UBYTE) OFFSET(Node,    ln_Type),      NT_LIBRARY,                0,
  245.  0xC000, (UBYTE) OFFSET(Node,    ln_Name),      LIBNAME,
  246.  0xA0,   (UBYTE) OFFSET(Library, lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED, 0,
  247.  0x90,   (UBYTE) OFFSET(Library, lib_Version),  VERSION,
  248.  0x90,   (UBYTE) OFFSET(Library, lib_Revision), REVISION,
  249.  0xC000, (UBYTE) OFFSET(Library, lib_IdString), IDSTRING,
  250.  0
  251. };
  252.  
  253. /* The following data structures and data are responsible for
  254.    setting up the Library base data structure and the library
  255.    function vector.
  256. */
  257. const ULONG LibInitTable[4] = {
  258.   (ULONG)sizeof(struct ExampleBaseP), /* Size of the base data structure */
  259.   (ULONG)LibVectors,             /* Points to the function vector */
  260.   (ULONG)&LibInitData,           /* Library base data structure setup table */
  261.   (ULONG)LibInit                 /* The address of the routine to do the setup */
  262. };
  263.  
  264. #endif /* EXAMPLE_LIBINIT_C */
  265.